package com.mobilesales.android.activities;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Spinner;

import com.mobilesales.android.c2dm.C2DMMessaging;
import com.mobilesales.android.c2dm.NotificationReceiver;

public abstract class ModelActivity<T> extends ActionBarActivity implements
		IRefreshable {

	private T model;
	private NotificationReceiver receiver;

	protected void setModel(T model) {
		this.model = model;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		receiver = new NotificationReceiver() {
			@Override
			public void onMessage(String msg) {
				refresh();
			}
		};
		C2DMMessaging.addNotificationReceiver(receiver);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (receiver != null)
			C2DMMessaging.removeNotificationReceiver(receiver);
	}

	protected T getModel() {
		return this.model;
	}

	protected boolean validate() {
		return true;
	}

	public void finish(View v) {
		finish();
	}

	public void fixSpinner(Spinner spinner) {
		DisplayMetrics d = new DisplayMetrics();
		getWindow().getWindowManager().getDefaultDisplay().getMetrics(d);
		int wdt = d.widthPixels;
		spinner.getLayoutParams().width = wdt - 10;
	}

	private Handler handler = new Handler();

	protected Handler getHandler() {
		return this.handler;
	}

}